﻿using Direct.IDAL.User.Money;
using DirectSell.Model.Money;
using DirectSell.Model.Query;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Webdiyer.WebControls.Mvc;

namespace DirectSell.DAL.User.Money
{
    /// <summary>
    /// 用户转账仓储类
    /// </summary>
    public class UserGiroRepository:AbsRepository, IUserGiroRepository
    {
        #region 初始化数据
        /// <summary>
        /// 电子币
        /// </summary>
        protected VirtualMoneyRepository virtualMoney { get { return new VirtualMoneyRepository(); } }
        #endregion

        #region 查询电子币余额
        /// <summary>
        /// 查询电子币余额
        /// </summary>
        /// <param name="usercode"></param>
        /// <returns></returns>
        public decimal QueryVirtualMoneyBanlace(string usercode)
        {
            return virtualMoney.QueryVirtualMoney(usercode);
        }
        #endregion

        #region 转出人扣电子币
        /// <summary>
        /// 转出人扣电子币
        /// </summary>
        /// <param name="UserCode"></param>
        public void Hushmoney(string UserCode,decimal money,ref Hashtable hstable)
        {
            StringBuilder sb = new StringBuilder();
            sb.AppendFormat(" UPDATE UserInforMations SET VirtualMoney-={0} ", money);
            sb.AppendFormat(" WHERE UserCode='{0}' ", UserCode);
            hstable.Add(sb.ToString(), null);
        }
        #endregion

        #region 接受人增加电子币
        /// <summary>
        /// 接收人增加电子币
        /// </summary>
        /// <param name="UserCode"></param>
        /// <param name="money"></param>
        /// <param name="hstable"></param>
        public void AddMoney(string UserCode, decimal money, ref Hashtable hstable)
        {
            StringBuilder sb = new StringBuilder();
            sb.AppendFormat(" UPDATE UserInforMations SET VirtualMoney+={0} ", money);
            sb.AppendFormat(" WHERE UserCode='{0}' ", UserCode);
            hstable.Add(sb.ToString(), null);
        }
        #endregion

        #region 写入转账记录表
        /// <summary>
        /// 写入转账记录表
        /// </summary>
        public void InsertGiro(UserGiroReq req, ref Hashtable hstable)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append(" INSERT INTO UserGiro ");
            sb.Append(" ([UserCode] ");
            sb.Append(" ,[UserName] ");
            sb.Append(" ,[Amount] ");
            sb.Append(" ,[ReceiveCode] ");
            sb.Append(" ,[Type] ");
            sb.Append(" ,[CreateTime] ");
            sb.Append(" ,[Remark] ");
            sb.Append(" ,[ReceiveName]) ");
            sb.Append("  VALUES ");
            sb.AppendFormat(" ('{0}' ", req.UserCode);
            sb.AppendFormat(" ,'{0}' ", req.UserName);
            sb.AppendFormat(" ,'{0}' ", req.Amount);
            sb.AppendFormat(" ,'{0}' ", req.ReceiveCode);
            sb.AppendFormat(" ,{0} ", req.Type);
            sb.AppendFormat(" ,'{0}' ", req.CreateTime);
            sb.AppendFormat(" ,'{0}' ", req.Remark);
            sb.AppendFormat(" ,'{0}')  ", req.ReceiveName);
            hstable.Add(sb.ToString(), null);

        }
        #endregion

        #region 判断接收会员是否存在
        /// <summary>
        /// 判断接收会员是否存在
        /// </summary>
        /// <param name="usercode"></param>
        /// <returns></returns>
        public bool CheckIsExcits(string usercode)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append(" SELECT COUNT(*) FROM UserInforMations ");
            sb.AppendFormat(" WHERE UserCode='{0}' AND UserStatus=1 ",usercode);
            return base.QueryFirstT<int>(sb.ToString())>0;
        }
        #endregion

        #region 查转出记录
        /// <summary>
        /// 查转出记录
        /// </summary>
        /// <returns></returns>
        public async Task<PagedList<UserGiro>> GetHushRecord(string usercode,int pageindex,int pagesize)
        {
            PageParam model = new PageParam()
            {
                OrderBy = "Id DESC",
                PageIndex = pageindex,
                PageSize = pagesize,
                StrWhere = $"UserCode='{0}'",
                TableName = "UserGiro"
            };
            return await base.PageQuery<UserGiro>(model);
        }
        #endregion

        #region 查转入记录
        /// <summary>
        /// 查转入记录
        /// </summary>
        /// <param name="usercode"></param>
        /// <param name="pageindex"></param>
        /// <param name="pagesize"></param>
        /// <returns></returns>
        public async Task<PagedList<UserGiro>> GetReciveRecord(string usercode,int pageindex,int pagesize)
        {
            PageParam model = new PageParam()
            {
                OrderBy = "Id DESC",
                PageIndex = pageindex,
                PageSize = pagesize,
                StrWhere = $"ReceiveCode='{0}'",
                TableName = "UserGiro"
            };
            return await base.PageQuery<UserGiro>(model);
            //StringBuilder sb = new StringBuilder();
            //sb.Append(" SELECT * FROM  UserGiro ");
            //sb.AppendFormat(" WHERE ReceiveCode='{0}' ", usercode);
            //var result = await base.QueryAsync<UserGiro>(sb.ToString());
            //return result as IList<UserGiro>;
        }
        #endregion



    }
}
